package xyz.zhzby.cyzx.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xyz.zhzby.cyzx.acl.dto.request.UserPageRequest;
import xyz.zhzby.cyzx.acl.dto.response.UserResponse;
import xyz.zhzby.cyzx.acl.entity.User;
import xyz.zhzby.cyzx.acl.service.IRoleService;
import xyz.zhzby.cyzx.acl.service.IUserService;
import xyz.zhzby.cyzx.servicebase.domain.PageResponse;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;
import xyz.zhzby.cyzx.servicebase.utils.MDUtil;

import java.util.List;
import java.util.Map;


/**
 * @author zby
 * @date 2021/12/07
 */
@RestController
@RequestMapping("/acl/user")
//@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "新增管理用户")
    @PostMapping("")
    public RestResponse<String> save(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @ApiOperation(value = "获取管理用户分页列表")
    @PostMapping("/list")
    public RestResponse<PageResponse<UserResponse>> index(@RequestBody UserPageRequest pageRequest) {
        return userService.getUsers(pageRequest);
    }

    @ApiOperation("根据Id查询用户")
    @GetMapping("/{userId}")
    public RestResponse<UserResponse> getUserById(@PathVariable("userId") Long userId) {
        return userService.getUserById(userId);
    }

    @ApiOperation("根据ID查询用户（供其他模块调用，n久后如果想重构，结构一定要提前定好，不然就像我现在只能这样加了...）")
    @GetMapping("/info/{userId}")
    public RestResponse<Map<String, Object>> getUserInfoById(@PathVariable("userId") Long userId) {
        return userService.getUserInfoById(userId);
    }

    @ApiOperation("根据ID列表查询用户（供其他模块调用，n久后如果想重构，结构一定要提前定好，不然就像我现在只能这样加了...）")
    @GetMapping("/info/list")
    public List<Map<String, Object>> getUserInfoByIdList(@RequestParam("userIds") List<Long> userIds) {
        return userService.getUserInfoByIdList(userIds);
    }

    @ApiOperation("修改用户信息")
    @PutMapping("")
    public RestResponse<String> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/assign/{userId}")
    public RestResponse<Map<String, Object>> toAssign(@PathVariable Long userId) {
        return roleService.findRoleByUserId(userId);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/assign")
    public RestResponse<String> doAssign(@RequestParam Long userId, @RequestParam Long[] roleId) {
        roleService.saveUserRoleRealtionShip(userId, roleId);
        return RestResponse.success("分配成功");
    }

    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/{userId}")
    public RestResponse<String> deleteUser(@PathVariable("userId") Long userId) {
        return userService.deleteUser(userId);
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("/list")
    public RestResponse<String> deleteUserBatch(@RequestBody List<Long> userIds) {
        return userService.deleteUserBatch(userIds);
    }
}

